package com.back.end.Service.impl;

import com.back.end.Dao.WebProductDao;
import com.back.end.Modal.ProductChild;
import com.back.end.Modal.CategoriesChild;
import com.back.end.Modal.Sales;
import com.back.end.Service.WebProductService;
import com.back.end.enums.ResultCode;
import com.back.end.exception.AuthException;
import io.netty.handler.codec.CodecException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.IllegalFormatException;
import java.util.List;

@Service
public class WebProductServiceImpl implements WebProductService {

    @Resource
    WebProductDao webProductDao;

    @Override
    public List<ProductChild> getProductList() {
        List<ProductChild> productChildList = webProductDao.getProductList();
        return productChildList;
    }

    @Override
    public ProductChild getProductById(Integer id) {
        ProductChild productChild = webProductDao.getProductChildById(id);
        return productChild;
    }

    @Override
    public ProductChild getProductByName(String name) {
        ProductChild productChild = webProductDao.getProductChildByName(name);
        return productChild;
    }

    @Override
    public List<CategoriesChild> getProductTypes() {
        List<CategoriesChild> productTypeList = webProductDao.getProductTypes();
        return productTypeList;
    }

    @Override
    public CategoriesChild getCategoryById(Integer id) {
        CategoriesChild category = webProductDao.getCategoryById(id);
        return category;
    }

    @Override
    public void updateCategory(CategoriesChild category) {
        webProductDao.updateCategory(category.getId(), category.getName(), category.getCreatedTime(), category.getUpdatedTime());
    }

    @Override
    public String addCategory(CategoriesChild category) {
        CategoriesChild categoriesChild = webProductDao.getCategoryByName(category.getName());
        if (categoriesChild != null) {
            throw new AuthException(ResultCode.NAME_ERROR);
        }
        webProductDao.addCategory(category);
        return "success";
    }

    @Override
    public String addProduct(ProductChild productChild) {
        ProductChild product = webProductDao.getProductChildById(productChild.getId());
        if (product != null) {
            return "商品名称已存在";
        }
        webProductDao.addProduct(productChild);
        return "成功";
    }

    @Override
    public void updateProduct(ProductChild productChild) {
        webProductDao.updateProduct(productChild.getId(), productChild.getName(), productChild.getTypeId(), productChild.getType(), productChild.getPrice(), productChild.getDetail(), productChild.getImagePath(), productChild.getProductCode());
    }

    @Override
    public void dealProduct(ProductChild productChild) {
        webProductDao.dealProduct(productChild.getId(), productChild.getStatus());
    }

    @Override
    public List<Sales> getRankList(String time) {
        List<Sales> sales = new ArrayList<Sales>();
        List<Sales> result = new ArrayList<Sales>();
        List<Integer> idList = new ArrayList<Integer>();
        if (time.equals("LAST_MONTH")) {
            sales = webProductDao.getSalesLastMonth();
        } else if (time.equals("MONTH")) {
            sales = webProductDao.getSalesMonth();
        } else if (time.equals("LAST_WEEK")) {
            sales = webProductDao.getSalesLastWeek();
        }
        sales.forEach(item -> {
            if (!idList.contains(item.getProductId())) {
                idList.add(item.getProductId());
            }
        });
        idList.forEach(item -> {
            ProductChild productChild = webProductDao.getProductChildById(item);
            int sum = 0;
            if (time.equals("LAST_MONTH")) {
                sum = webProductDao.getSalesLastMonthById(item);
            } else if (time.equals("MONTH")) {
                sum = webProductDao.getSalesMonthById(item);
            } else if (time.equals("LAST_WEEK")) {
                sum = webProductDao.getSalesLastWeekById(item);
            }
            Sales sale = new Sales();
            sale.setNumber(sum);
            sale.setName(productChild.getName());
            sale.setProductId(item);
            sale.setImage(productChild.getImagePath());
            result.add(sale);
        });
        result.sort(Comparator.comparing(Sales::getNumber).reversed());
        List<Sales> res = new ArrayList<Sales>();
        if (result.size() >= 5) {
            res = result.subList(0, 5);
        } else {
            List<ProductChild> p = webProductDao.getProductList();
            for (int i = result.size(); i < 5; i++) {
                Sales s = new Sales();
                s.setImage(p.get(i).getImagePath());
                s.setNumber(0);
                s.setProductId(p.get(i).getId());
                s.setName(p.get(i).getName());
                result.add(s);
            }
            res = result;
        }
        return res;
    }

}
